#!/bin/sh

#
# Copyright (c) 2009, NICE s.r.l.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of NICE s.r.l. nor the names of its contributors may
#     be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#------------------------------------------------------------------------------
# install.sh: iRODS EnginFrame plugin installer
#
# Authors:
#   Paolo Borelli <paolo.borelli@nice-software.com>
#   Francesco Locunto <francesco.locunto@nice-software.com>
#------------------------------------------------------------------------------


# Configuration variables
EF_ROOT=
INSTALL_DEMO=


ask_question() {
    _question="$1"
    _varname="$2"
    _default="$3"
    _prompt="$4"
    _answer=""
     echo "${_question} [${_default}] "
     [ -n "${_prompt}" ] && printf "${_prompt} "
     read _answer
     [ -z "${_answer}" ] && _answer="${_default}"
     eval "${_varname}=\"${_answer}\""
     return 0
}

check_user() {
    _user=`id -u`
    if [ "${_user}" -ne 0 ] ; then
        echo "You must be root to perform installation."
        exit 1
    fi
    return 0
}

check_ef_root() {
    test -d "$1" -a -d "$1/plugins"
    return "$?"
}


ask_efroot() {
    _question="Specify the EF_ROOT directory of your Enginframe installation"
    _ef_root=""
    while ask_question "${_question}" "_ef_root" "/opt/nice/enginframe" "EF_ROOT Directory:"
    do
       if check_ef_root "${_ef_root}"; then
           EF_ROOT="${_ef_root}"
           break
       else
           echo "${_ef_root} is not a valid EnginFrame installation directory"
           EF_ROOT=""
       fi
    done
    return 0
}


ask_installdemo() {
    _question="Do you want to install a demo using the EnginFrame iRODS integration?" 
    while ask_question "${_question}" "_install_demo" "Yes"
    do
        case "${_install_demo}" in
        Yes|Y|y)
            INSTALL_DEMO="true"
            break
            ;;
        No|N|n)
            INSTALL_DEMO=""
            break
            ;;
        *)
            echo "Please specify Yes or No"
        esac
    done
    return 0
}


do_install() {
    (cp -R -f "plugins/irods" "${EF_ROOT}/plugins"
    chown -R root:root "${EF_ROOT}/plugins/irods"
    chmod -R 755 "${EF_ROOT}/plugins/irods"
    chmod 777 "${EF_ROOT}/plugins/irods/auth/sessions"
    chmod 644 "${EF_ROOT}/plugins/irods/auth/users.mapping" \
        "${EF_ROOT}/plugins/irods/conf/ef.irods.conf" \
        "${EF_ROOT}/plugins/irods/bin/common"
    
    cp -R -f "plugins/rspooler/bin/irods" "${EF_ROOT}/plugins/rspooler/bin/irods"
    chown -R root:root "${EF_ROOT}/plugins/rspooler/bin/irods"
    chmod -R 755 "${EF_ROOT}/plugins/rspooler/bin/irods") 2>&1
    
    if [ $? -ne 0 ] ; then
        echo "Error installing EnginFrame iRODS plugin: installation aborted."
        exit 1
    else
        echo "EnginFrame iRODS plugin installed correctly."
    fi

    if [ -n "INSTALL_DEMO" ]; then
        (cp -R -f "plugins/irods-demo" "${EF_ROOT}/plugins"
        chown -R root:root "${EF_ROOT}/plugins/irods-demo"
        chmod -R 755 "${EF_ROOT}/plugins/irods-demo"
        chmod 644 "${EF_ROOT}/plugins/irods-demo/WEBAPP/index.html" \
            "${EF_ROOT}/plugins/irods-demo/WEBAPP/irods-demo.xml" \
            "${EF_ROOT}/plugins/irods-demo/WEBAPP/images/logo.png" \
            "${EF_ROOT}/plugins/irods-demo/xsl/irods-demo.xsl") 2>&1
        
        if [ $? -ne 0 ] ; then
            echo "Error installing demo service for the EnginFrame iRODS plugin."
            exit 1
        else
            echo "Demo service for the EnginFrame iRODS plugin installed correctly."
        fi
    fi
}

main() {
    check_user
    ask_efroot
    ask_installdemo
    do_install    
}

main "$@"

# ex:ts=4:et:
